//
//  AmountDescViewController.swift
//  jiaoyi
//
//  Created by wjk on 2018/8/19.
//  Copyright © 2018年 wjk. All rights reserved.
//

import UIKit

class AmountDescViewController: BaseViewController, LTTableViewProtocal, UITableViewDataSource, UITableViewDelegate  {

    var count = 20;
    
    var datas:[NSAttributedString] = [NSAttributedString]()
    
    public lazy var tableView: UITableView = {
        let H: CGFloat = glt_iphoneX ? (view.bounds.height - 64 - 24 - 34) : view.bounds.height  - 64
        let tableView = tableViewConfig(CGRect(x: 0, y: 0, width: view.bounds.width, height: H), self, self, nil)
        tableView.rowHeight = 120
        return tableView
    }()
    
    override func viewDidLoad() {
        super.viewDidLoad()
        view.backgroundColor = UIColor.white
        view.addSubview(tableView)
        tableView.tableFooterView = UIView();
        
        tableView.register(R.nib.amountDescTableViewCell(), forCellReuseIdentifier: "amountDescTableViewCell")
        
        tableView.register(R.nib.emptyTableViewCell(), forCellReuseIdentifier: "emptyTableViewCell")
        
        
        glt_scrollView = tableView
        
        if #available(iOS 11.0, *) {
            tableView.contentInsetAdjustmentBehavior = .never
        } else {
            automaticallyAdjustsScrollViewInsets = false
        }
        tableView.separatorStyle = UITableViewCellSeparatorStyle.none
        
        if (UIApplication.shared.delegate as! AppDelegate).isplayData.count > 0 {
            tableView.rowHeight = 294
            self.datas.append(StyledTextBuilder(text: "")
                .save()
                .add(text: "0.00", traits: [.traitBold], attributes: [NSAttributedStringKey.foregroundColor:UIColor.red, NSAttributedStringKey.font:UIFont.systemFont(ofSize: 16)])
                .restore()
                .add(text: " 元", traits: [.traitBold], attributes: [NSAttributedStringKey.foregroundColor:UIColor.black, NSAttributedStringKey.font:UIFont.systemFont(ofSize: 14)])
                .restore()
                .build()
                .render(contentSizeCategory: .large))
            
            self.datas.append(StyledTextBuilder(text: "")
                .save()
                .add(text: "0.00", traits: [.traitBold], attributes: [NSAttributedStringKey.foregroundColor:UIColor.red, NSAttributedStringKey.font:UIFont.systemFont(ofSize: 16)])
                .restore()
                .add(text: " 元", traits: [.traitBold], attributes: [NSAttributedStringKey.foregroundColor:UIColor.black, NSAttributedStringKey.font:UIFont.systemFont(ofSize: 14)])
                .restore()
                .build()
                .render(contentSizeCategory: .large))
            
            self.datas.append(StyledTextBuilder(text: "")
                .save()
                .add(text: "0.00", traits: [.traitBold], attributes: [NSAttributedStringKey.foregroundColor:UIColor.red, NSAttributedStringKey.font:UIFont.systemFont(ofSize: 16)])
                .restore()
                .add(text: " 元", traits: [.traitBold], attributes: [NSAttributedStringKey.foregroundColor:UIColor.black, NSAttributedStringKey.font:UIFont.systemFont(ofSize: 14)])
                .restore()
                .build()
                .render(contentSizeCategory: .large))
            self.datas.append(StyledTextBuilder(text: "")
                .save()
                .add(text: "0.00", traits: [.traitBold], attributes: [NSAttributedStringKey.foregroundColor:UIColor.red, NSAttributedStringKey.font:UIFont.systemFont(ofSize: 16)])
                .restore()
                .add(text: " 元", traits: [.traitBold], attributes: [NSAttributedStringKey.foregroundColor:UIColor.black, NSAttributedStringKey.font:UIFont.systemFont(ofSize: 14)])
                .restore()
                .build()
                .render(contentSizeCategory: .large))
            self.tableView.reloadData()
        }else {
            tableView.rowHeight = 120
            loadData()
        }
        
        
    }
    
    func loadData() -> Void {
        if (UIApplication.shared.delegate as! AppDelegate).isplayData.count > 0 {
            return
        }
        NetWorkRequest(.accountdetail()) { (res) -> (Void) in
            if res["code"] as! String == "000000" {
                let data = jsonFormater(data:(res["data"]!))
                let formater = NumberFormatter()
                
                formater.numberStyle = .decimal
                formater.minimumFractionDigits = 2
                formater.maximumFractionDigits = 2
                self.datas.append(StyledTextBuilder(text: "")
                    .save()
                    .add(text: formater.string(from: NSNumber(value:data["available_fund"].doubleValue / 100.0))!, traits: [.traitBold], attributes: [NSAttributedStringKey.foregroundColor:UIColor.red, NSAttributedStringKey.font:UIFont.systemFont(ofSize: 16)])
                    .restore()
                    .add(text: " 元", traits: [.traitBold], attributes: [NSAttributedStringKey.foregroundColor:UIColor.black, NSAttributedStringKey.font:UIFont.systemFont(ofSize: 14)])
                    .restore()
                    .build()
                    .render(contentSizeCategory: .large))
                
                self.datas.append(StyledTextBuilder(text: "")
                    .save()
                    .add(text: formater.string(from: NSNumber(value:data["frozen_payment"].doubleValue / 100.0))!, traits: [.traitBold], attributes: [NSAttributedStringKey.foregroundColor:UIColor.red, NSAttributedStringKey.font:UIFont.systemFont(ofSize: 16)])
                    .restore()
                    .add(text: " 元", traits: [.traitBold], attributes: [NSAttributedStringKey.foregroundColor:UIColor.black, NSAttributedStringKey.font:UIFont.systemFont(ofSize: 14)])
                    .restore()
                    .build()
                    .render(contentSizeCategory: .large))
                let c = data["frozen_trade_deposit"].doubleValue + data["frozen_perform_deposit"].doubleValue
                self.datas.append(StyledTextBuilder(text: "")
                    .save()
                    .add(text: formater.string(from: NSNumber(value: c / 100.0))!, traits: [.traitBold], attributes: [NSAttributedStringKey.foregroundColor:UIColor.red, NSAttributedStringKey.font:UIFont.systemFont(ofSize: 16)])
                    .restore()
                    .add(text: " 元", traits: [.traitBold], attributes: [NSAttributedStringKey.foregroundColor:UIColor.black, NSAttributedStringKey.font:UIFont.systemFont(ofSize: 14)])
                    .restore()
                    .build()
                    .render(contentSizeCategory: .large))
                self.datas.append(StyledTextBuilder(text: "")
                    .save()
                    .add(text: formater.string(from: NSNumber(value:data["temp_frozen_fund"].doubleValue / 100.0))!, traits: [.traitBold], attributes: [NSAttributedStringKey.foregroundColor:UIColor.red, NSAttributedStringKey.font:UIFont.systemFont(ofSize: 16)])
                    .restore()
                    .add(text: " 元", traits: [.traitBold], attributes: [NSAttributedStringKey.foregroundColor:UIColor.black, NSAttributedStringKey.font:UIFont.systemFont(ofSize: 14)])
                    .restore()
                    .build()
                    .render(contentSizeCategory: .large))
                self.tableView.reloadData()
            }
        }
    }
    
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        if self.datas.count > 0 {
            return 1
        }
        return 1
    }
    
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        
        if (UIApplication.shared.delegate as! AppDelegate).isplayData.count > 0 {
            
            let cell = tableView.dequeueReusableCell(withIdentifier: "emptyTableViewCell") as! EmptyTableViewCell

            cell.selectionStyle = .none

            cell.labelContent.text = "暂无符合条件的信息"
            
            return cell
            
        }
        
        let cell = tableView.dequeueReusableCell(withIdentifier: "amountDescTableViewCell") as! AmountDescTableViewCell
        cell.selectionStyle = UITableViewCellSelectionStyle.none
        if self.datas.count > 0 {
            
        
        cell.label1.attributedText = self.datas[0]
        cell.label2.attributedText = self.datas[2]
        cell.label3.attributedText = self.datas[1]
        cell.label4.attributedText = self.datas[3]
          
        }
        return cell
    }
    func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        tableView.deselectRow(at: indexPath, animated: true)
    }
    /*
    // MARK: - Navigation

    // In a storyboard-based application, you will often want to do a little preparation before navigation
    override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
        // Get the new view controller using segue.destinationViewController.
        // Pass the selected object to the new view controller.
    }
    */

}
